package com.dhq.myapp.util

import android.util.Log
import com.rokid.cxr.client.extend.CxrApi
import com.rokid.cxr.client.extend.callbacks.WifiP2PStatusCallback
import com.rokid.cxr.client.utils.ValueUtil

/**
 * DESC
 * Created by DHQ on 2025/10/11.
 */
open class WifiUtil(val mWifiListener:WifiListener?=null) {


    private val TAG = "WifiUtil"
    /**
     * Init Wifi
     *
     * @return  Status:
     * @see ValueUtil.CxrStatus
     * @see ValueUtil.CxrStatus.REQUEST_SUCCEED Success
     * @see ValueUtil.CxrStatus.REQUEST_WAITING Waiting
     * @see ValueUtil.CxrStatus.REQUEST_FAILED Failed
     */
    fun initWifi(): ValueUtil.CxrStatus?{
        return CxrApi.getInstance().initWifiP2P(object : WifiP2PStatusCallback {
            /**
             * Connected
             */
            override fun onConnected() {
                Log.d(TAG, "onConnected")
                mWifiListener?.onConnected()
            }

            /**
             * Disconnected
             */
            override fun onDisconnected() {
                Log.d(TAG, "onDisconnected")
                mWifiListener?.onDisconnected()
            }

            /**
             * Failed
             *
             * @param errorCode   Error Code:
             * @see ValueUtil.CxrWifiErrorCode
             * @see ValueUtil.CxrWifiErrorCode.WIFI_DISABLED Mobile Phone Wi-Fi disabled
             * @see ValueUtil.CxrWifiErrorCode.WIFI_CONNECT_FAILED Wi-Fi P2P Connect Failed
             * @see ValueUtil.CxrWifiErrorCode.UNKNOWN Unknown
             */
            override fun onFailed(errorCode: ValueUtil.CxrWifiErrorCode?) {
                mWifiListener?.onFailed()
            }

        })
    }


    fun connect(wifiListener:WifiListener){
        CxrApi.getInstance().initWifiP2P(object : WifiP2PStatusCallback {
            /**
             * Connected
             */
            override fun onConnected() {
                Log.d(TAG, "onConnected")
                wifiListener.onConnected()
            }

            /**
             * Disconnected
             */
            override fun onDisconnected() {
                Log.d(TAG, "onDisconnected")
                wifiListener.onDisconnected()
            }

            /**
             * Failed
             *
             * @param errorCode   Error Code:
             * @see ValueUtil.CxrWifiErrorCode
             * @see ValueUtil.CxrWifiErrorCode.WIFI_DISABLED Mobile Phone Wi-Fi disabled
             * @see ValueUtil.CxrWifiErrorCode.WIFI_CONNECT_FAILED Wi-Fi P2P Connect Failed
             * @see ValueUtil.CxrWifiErrorCode.UNKNOWN Unknown
             */
            override fun onFailed(errorCode: ValueUtil.CxrWifiErrorCode?) {
                wifiListener.onFailed()
            }

        })
    }



    /**
     * Get Wifi Connection Status
     *
     * @return  Connection Status: true-connected, false-disconnected
     */
    fun getWiFiConnectionStatus(): Boolean{
        return CxrApi.getInstance().isWifiP2PConnected
    }


    /**
     * Deinit Wifi
     */
    private fun deinitWifi(){
        CxrApi.getInstance().deinitWifiP2P()
    }

    interface WifiListener{
        fun onConnected()
        fun onDisconnected()
        fun onFailed()
    }
}